#!/usr/bin/env python3

import random

#   随机1-100之间的数，由用户输入猜测，用户输入的数大则提示大，小则提示小

def randomNum():
    randomNum = random.randint(1, 101)
    round = 0
    while round < 5:
        num = int(input("请选择一个数字:"))
        if num < 1 or num > 101:
            print('您输入的数字超出了范围！')
        elif num < randomNum:
            print('您输入的数字小了。')
        elif num > randomNum:
            print('您输入的数字大了。')
        elif  num == randomNum:
            print('恭喜你，回答正确！')
        round += 1
        print('您的机会还有', (5 - round), '次')
        while round == 5:
            print('很遗憾，你的机会用完了，请重头再来！')
            break

#   求1000以内的完数，完数的定义：所有因子之和等于该本身
#def perfectNum()

#   判断一个数是否为回文数(11 12321 1234321)

def palindrome():
    num = str(input('请输入一个数：'))

    if num[::-1] == num:
        print('True')
    else:
        print ('False')


#  打印矩形

def rect(w, h):
    print("w = %d, h = %d" % (w, h))
    for r in range(h):
        for c in range(w):
            print(" *", end='')
        print()

    print("=======================")

    for r in range(h):
        print(" *" * w)

    print("=======================")
    for i in range(w * h):
        c = i % w
        print(" *", end='')
        if c == w - 1:
            print()

#   打印左上三角形(输入边长打印直角三角形用一个循环)

def triangle1():
    length = int(input('请输入边长: '))
    for i in range(length, 0, -1):
        print(' *' * i, ' ' * (length - i))
#   打印左下三角形(输入边长打印直角三角形用一个循环)

def triangle2():
    length = int(input('请输入边长: '))
    for i in range(length, 0, -1):
        print(' *' * (length - i + 1), ' ' * i)

#打印右上三角形(输入边长打印直角三角形用一个循环)

def triangle3():
    length = int(input('请输入边长: '))
    for i in range(length, 0, -1):
        print('  ' * (length - i), ' *' * i)

#打印右下三角形(输入边长打印直角三角形用一个循环)

def triangle4():
    length = int(input('请输入边长: '))
    for i in range(length, 0, -1):
        print('  ' * i, ' *' * (length - i + 1))

#打印梯形 3 5

def trapezium():
    for trapezium in range(3, 12):
        if trapezium % 2 != 0:
            print(' ' * (12 - trapezium), ' *' * trapezium)
        elif trapezium % 2 == 0:
            continue

#用一个循环打印菱形，如输出3打印

def rhombus():
    length = int(input('请输入边长(输入五):'))

    for rhombus in range(0, length):
        if rhombus % 2 != 0:
            print(' ' * (length - rhombus), ' *' * rhombus)
        elif rhombus % 2 == 0:
            continue

        while rhombus == length - 2:
            for rhombus in range(length, 0, -1):
                if rhombus % 2 != 0:
                    print(' ' * (length - rhombus), ' *' * rhombus)
                elif rhombus % 2 == 0:
                    continue



#控制菜单：
while True:
    print("1. 随机猜数(范围:1-100)")
    print("2. 回文数")
    print("3. 矩形")
    print("4. 三角形")
    print("5. 梯形")
    print("6. 菱形")
    print("7. 退出")
    sel = int(input("请输入【1 - 7】: "))
    print("------------------------------------------------------------")
    if sel == 1:
        randomNum()
    elif sel == 2:
        palindrome()
    elif sel == 3:
        rect(random.randint(1, 10), random.randint(1, 10))
    elif sel == 4:
        while True:
            print("1.左上直角三角形")
            print("2.左下直角三角形")
            print("3.右上直角三角形")
            print("4.右下直角三角形")
            print("5.返回上一级")
            sel = int(input("请输入【1 - 5】:"))
            print("----------------------------------------------------")
            if sel == 1:
                triangle1()
            elif sel == 2:
                triangle2()
            elif sel == 3:
                triangle3()
            elif sel == 4:
                triangle4()
            elif sel == 5:
                break
    elif sel == 5:
        trapezium()
    elif sel == 6:
        rhombus()
    elif sel == 7:
        break
    print("-----------------------------------------------------------")
